package com.future.bantang.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.future.bantang.R;

/**
 * Created by Administrator on 16-1-12.
 */
public class NaviView extends RadioGroup {


    //导航圆点选择器资源id，如果不设置将使用默认选择器,
    //如果设置需要在调用setCount和setCurIndex方法前设置
    private int mSelectorRes=-1;
    //设置导航圆点的属性
    private LayoutParams mParams;
    //导航圆点的数量
    private int count=0;




    //设置当前选中的圆点的索引
    private int curIndex=0;

    public NaviView(Context context) {
        this(context, null);
    }

    public NaviView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setLayoutParams(params);
        this.setOrientation(HORIZONTAL);
        this.setGravity(Gravity.CENTER);
        this.setPadding(10, 10, 10, 10);
        mParams=new LayoutParams(10,10);
        mParams.setMargins(0, 0, 15, 0);
        mSelectorRes= R.drawable.selector_naviview_indicator;

    }

    /**
     * 设置指示器资源
     * @param mSelectorRes
     */
    public void setSelectorRes(int mSelectorRes) {
        this.mSelectorRes = mSelectorRes;
    }

    /**
     * 设置导航圆点的数量
     * @param count
     */
    public void setCount(int count) {
        reSetCount(count);
        this.count = count;
    }

    /**
     * 设置当前选择的索引值
     * @param curIndex
     */
    public void setCurIndex(int curIndex) {
        if (curIndex>=this.count){
            return;
        }
        this.curIndex = curIndex;
        RadioButton radioButton= (RadioButton) this.getChildAt(curIndex);
        radioButton.setChecked(true);
    }


    /**
     * 重置导航圆点的数量
     * @param count
     */
    private void reSetCount(int count) {
        for (int i = this.count; i > count ; i--) {
            this.removeViewAt(i-1);
        }

        for (int i = this.count; i < count ; i++) {
            addIndicator(i);
        }
    }

    private ColorDrawable colorDrawable =new ColorDrawable(Color.TRANSPARENT);
    private void addIndicator(int position) {
        RadioButton radioButton=new RadioButton(getContext());
        radioButton.setLayoutParams(mParams);
        radioButton.setButtonDrawable(colorDrawable);
        radioButton.setBackgroundResource(mSelectorRes);
        this.addView(radioButton, position);
    }



}
